<template>
  <div class="machine-repair">
    <div>
      <label>{{ $t("route.Personnel_management") }}</label>
      <div></div>
      <button>
        <img /><label>{{ $t("route.Working_position") }}</label>
      </button>
    </div>

    <div>
      <div>
        <el-cascader :options="options" @change="proLineChange" />
        <el-pagination
          style="float: right"
          background
          @current-change="handlePageChange"
          :current-page="query.pageNum"
          :page-size="query.pageSize"
          layout="total, prev, pager, next"
          :total="query.total"
        ></el-pagination>
      </div>
      <div>
        <div v-if="chartShow" id="echart-1" style="height: 250px; width: 100%" />
      </div>
    </div>

    <div>
      <div>
        <div class="div-handle-right">
          <el-button @click="searchClick">{{ $t("route.search") }}</el-button>
          <input v-model="query.search" :placeholder="$t('route.Name_of_mechanic')" />
        </div>
      </div>

      <el-table class="table" :data="userWorkList" style="width: 100%">
        <el-table-column prop="nickname" :label="$t('route.full_name')" />
        <!-- <el-table-column prop="craft" :label="$t('route.Operation_procedure')" /> -->
        <el-table-column :label="$t('route.Clocking_time')" />
        <el-table-column width="300" :label="$t('route.Startup_duration')">
          <template #default="scope">
            {{ formatDur(scope.row.bootUpDur) }}/ {{ scope.row.deviceNum }}台设备
          </template>
        </el-table-column>
        <el-table-column width="300" :label="$t('route.Sewing_time')">
          <template #default="scope">
            {{ formatDur(scope.row.workDur) }}/ {{ scope.row.deviceNum }}台设备
          </template>
        </el-table-column>
        <el-table-column :label="$t('route.Equipment_turnover')">
          <template #default="scope">
            {{
              scope.row.bootUpDur
                ? ((scope.row.workDur / scope.row.bootUpDur) * 100).toFixed(2) + "%"
                : "0%"
            }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('route.Qualified_or_not')">
          <template #default="scope">
            {{ formatResult(scope.row) }}
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import http from "../../js/http/http.js";
import api from "../../js/http/api.js";
import CommonUtils from "../../js/common/common-utils.js";
import * as echarts from "echarts";
import Constant from "../../js/common/constant.js";
import i18n from "../../i18n";
export default {
  data() {
    return {
      chartShow: false,
      dataMonth: new Date(),
      query: {
        pageNum: 1,
        pageSize: 30,
        total: 0,
        search: null,
        workshop: null,
        proLine: null,
      },
      userWorkList: [],
      options: [],
    };
  },
  methods: {
    proLineChange(value) {
      this.query.workshop = value[0];
      this.query.proLine = value[1];
      this.query.search = null;
      this.getAllUserWork();
    },
    formatDur(dur) {
      if (!dur) return "0s";
      return CommonUtils.formatDur(dur);
    },
    formatResult(item) {
      if (item.bootUpDur == 0) return this.$t("route.no");
      if (item.workDur / item.bootUpDur < 0.5) {
        return this.$t("route.no");
      } else {
        return this.$t("route.yes");
      }
    },
    getAllUserWork() {
      http.post(
        api.GET_ALL_USER_WORK,
        {
          adminId: this.$store.state.user.id,
          factory: Constant.FACTORY == "全部工厂" ? null : Constant.FACTORY,
          workshop: this.query.workshop,
          proLine: this.query.proLine,
          pageNum: this.query.pageNum,
          pageSize: this.query.pageSize,
          search: this.query.search,
        },
        (data) => {
          var xData = [];
          var yData = [];
          for (var item of data.data.userWorkList) {
            xData.push(item.nickname);
            if (item.bootUpDur) {
              yData.push(((item.workDur / item.bootUpDur) * 100).toFixed(2));
            } else {
              yData.push(0);
            }
          }
          this.initEchart1(xData, yData);
          this.userWorkList = data.data.userWorkList;
          this.query.total = data.data.total;
        }
      );
    },
    handlePageChange(pageNum) {
      this.query.pageNum = pageNum;
      this.getAllUserWork();
    },
    initEchart1(xData, yData) {
      var myChart = echarts.init(document.getElementById("echart-1"));
      var option = CommonUtils.initBarOption(
        this.$t("route.Employee_activity"),
        xData,
        yData
      );
      myChart.setOption(option);
    },
    searchClick() {
      this.query.workshop = null;
      this.query.proLine = null;
      this.query.pageNum = 1;
      this.getAllUserWork();
    },

    getPorLineRow() {
      http.post(
        api.GET_POR_LINE_ROW,
        {
          adminId: this.$store.state.user.id,
        },
        (data) => {
          this.options = data.data;
        }
      );
    },
    select1Change() {
      this.query.pageNum = 1;
      this.getAllUserWork();
    },
  },
  created() {
    this.chartShow = true;
    this.getPorLineRow();
    this.getAllUserWork();
  },

  mounted() {},
};
</script>
<style lang="scss" scoped>
.machine-repair {
  color: #444444;
  font-size: 18px;
  padding-right: 20px;
  height: calc(100vh - 100px);
  // overflow: scroll;
  scrollbar-width: none;
  /* firefox */
  -ms-overflow-style: none;
  /* IE 10+ */
  overflow-x: hidden;
  overflow-y: auto;

  > div:not(:nth-child(2n)) {
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;
  }

  .select {
    width: 120px;
    margin-left: 10px;
  }

  > div:nth-of-type(1) {
    > label:nth-of-type(1) {
      font-weight: 600;
    }

    > div:nth-of-type(1) {
      width: 1px;
      background-color: #cccccc;
      height: 15px;
      margin: 0px 20px;
    }

    > button:nth-of-type(1) {
      background-image: url(../../assets/img/customer-1.png);
      background-repeat: no-repeat;
      background-size: 100% 100%;
      padding: 5px 10px;
      border: 0px;
      display: flex;
      color: #ffffff;
      flex-direction: row;
      align-items: center;
      font-weight: 800;

      > img {
        margin-right: 7px;
        width: 15px;
        height: 15px;
        content: url(../../assets/img/customer-2.png);
      }
    }
  }

  > div:nth-of-type(2) {
    margin-top: 20px;

    > div:nth-of-type(1) {
      background-color: #ffffff;
      padding: 10px;
      margin-bottom: 10px;
    }

    > div:nth-of-type(2) {
      width: 100%;
      display: flex;

      > div {
        background-color: #ffffff;
        border-radius: 5px;
        flex: 1;
        padding: 20px;
      }
    }
  }

  > div:nth-of-type(3) {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    margin-top: 20px;
    background-color: #ffffff;

    > div {
      width: 100%;
    }

    > div:nth-of-type(1) {
      display: flex;
      flex-direction: row;

      .div-handle-right {
        flex: 1;
        flex-direction: row-reverse;
        text-align: right;
        display: flex;
        align-items: center;

        > input:nth-of-type(1) {
          border: 1px solid #eeeeee;
          font-size: 15px;
          width: 150px;
          padding: 5px 8px;
          outline: none;
        }

        > button {
          color: #a866ff;
          margin-left: 10px;
          padding: 4px 20px;
        }
      }
    }
  }
}
</style>
